﻿using SharpSoft.WPF;
using SharpSoft.WPF.Adorners;
using SharpSoft.WPF.Masks;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace System
{
    public static class Extensions
    {
        #region Mask

        /// <summary>
        /// 创建并立即打开一个遮罩层。
        /// </summary>
        /// <param name="visual"></param>
        /// <param name="content">要显示的提示内容</param>
        /// <param name="timeout">设置显示时长，为null则会一直显示，直到调用<see cref="SharpSoft.WPF.Masks.Mask"/>的<see cref="SharpSoft.WPF.Masks.Mask.Close"/>方法。</param>
        /// <returns></returns>
        public static Mask Mask(this UIElement visual, object content, bool setFocus = true, Action<MaskLayout> setMaskLayout = null, TimeSpan? timeout = null)
        { 
            AdornerLayer adornerLayer = GetAdornerLayer(visual);
            if (adornerLayer == null)
            {//在UI上查找装饰器层失败，尝试在UI所在的窗体上查找装饰层。
                adornerLayer = GetAdornerLayer(visual.GetWindow());
            }
            if (adornerLayer == null)
            {
                throw new CanNotCreateAdornerException("未能在此UI上创建装饰器，尝试在此UI所在窗体上创建装饰器依然失败。");
            }

            var ml = new MaskLayout() { SetFocusOnMask = setFocus };
            setMaskLayout?.Invoke(ml);
            ml.MaskContent = content;
            var VA = new VisualAdorner(adornerLayer) { ContentVisual = ml };
            var maskHost = new Mask(VA, adornerLayer, timeout);
            if (content != null)
            {
                var type = content.GetType();
                var hostProp = type.GetProperty("MaskHost", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                if (hostProp != null)
                {
                    hostProp.SetValue(content, maskHost);
                }
            }
            return maskHost;
        }

        private static AdornerDecorator GetAdornerDecorator(this UIElement visual)
        {
            var Decorator = VisualHelper.FindChild<AdornerDecorator>(visual);
            return Decorator;

        }

        private static AdornerLayer GetAdornerLayer(Visual visual)
        {
            if (visual == null) return null;
            if (visual is AdornerDecorator ad)
            {
                return ad.AdornerLayer;
            }
            AdornerLayer adornerLayer = null;
            var childCount = VisualTreeHelper.GetChildrenCount(visual);
            if (childCount == 0)
            {
                return null;
            }
            for (int i = 0; i < childCount; i++)
            {
                var Cvisual = VisualTreeHelper.GetChild(visual, i) as Visual;
                if (Cvisual is AdornerDecorator ad1)
                {
                    return ad1.AdornerLayer;
                }
            }
            for (int i = 0; i < childCount; i++)
            {
                var Cvisual = VisualTreeHelper.GetChild(visual, i) as Visual;
                adornerLayer = GetAdornerLayer(Cvisual);
                if (adornerLayer != null)
                {
                    return adornerLayer;
                }
            }
            return null;

        }
        #endregion

        #region Toast


        #endregion

    }
}
